<template>
  <div class="app-container">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="订单名" prop="orderName" v-if="showSearch">
          <el-input
              v-model="queryParams.orderName"
              placeholder="请输入订单名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo" v-if="showSearch">
          <el-input
              v-model="queryParams.orderNo"
              placeholder="请输入订单编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="支付订单号" prop="payOrderNo" v-if="showSearch">
          <el-input
              v-model="queryParams.payOrderNo"
              placeholder="请输入支付订单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户" prop="customerId" v-if="showSearch">
          <el-input
              v-model="queryParams.customerId"
              placeholder="请输入客户"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单总金额" prop="orderTotalMoney" v-if="showSearch">
          <el-input
              v-model="queryParams.orderTotalMoney"
              placeholder="请输入订单总金额"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="购物金" prop="shopMoney" v-if="showSearch">
          <el-input
              v-model="queryParams.shopMoney"
              placeholder="请输入购物金"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="优惠金" prop="preMoney" v-if="showSearch">
          <el-input
              v-model="queryParams.preMoney"
              placeholder="请输入优惠金"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="商品应付金额" prop="payProductMoney" v-if="showSearch">
          <el-input
              v-model="queryParams.payProductMoney"
              placeholder="请输入商品应付金额"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="优惠券金" prop="couponMoney" v-if="showSearch">
          <el-input
              v-model="queryParams.couponMoney"
              placeholder="请输入优惠券金"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="实付金额" prop="payMoney" v-if="showSearch">
          <el-input
              v-model="queryParams.payMoney"
              placeholder="请输入实付金额"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="收货人" prop="buyerName" v-if="showSearch">
          <el-input
              v-model="queryParams.buyerName"
              placeholder="请输入收货人"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="buyerPhone" v-if="showSearch">
          <el-input
              v-model="queryParams.buyerPhone"
              placeholder="请输入联系电话"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="收货地址" prop="buyerAddress" v-if="showSearch">
          <el-input
              v-model="queryParams.buyerAddress"
              placeholder="请输入收货地址"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="邮费" prop="postagePrice" v-if="showSearch">
          <el-input
              v-model="queryParams.postagePrice"
              placeholder="请输入邮费"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button type="warning" icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button size="mini" @click="changSearch">{{showSearch?"常用条件":"所有条件"}}</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="back-item">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              type="primary"
              plain
              icon="Plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['mall:order:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="success"
              plain
              icon="Edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['mall:order:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="danger"
              plain
              icon="Delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['mall:order:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="warning"
              plain
              icon="Download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
              v-hasPermi="['mall:order:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="orderList"
          @selection-change="handleSelectionChange"
          @row-dblclick="handleDetails"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="订单Id" align="center" prop="orderId" v-if="columns[0].visible"/>
        <el-table-column label="订单名" align="center" prop="orderName" v-if="columns[1].visible"/>
        <el-table-column label="订单编号" align="center" prop="orderNo" v-if="columns[2].visible"/>
        <el-table-column label="支付订单号" align="center" prop="payOrderNo" v-if="columns[3].visible"/>
        <el-table-column label="客户" align="center" prop="customerId" v-if="columns[4].visible"/>
        <el-table-column label="订单总金额" align="center" prop="orderTotalMoney" v-if="columns[5].visible"/>
        <el-table-column label="购物金" align="center" prop="shopMoney" v-if="columns[6].visible"/>
        <el-table-column label="优惠金" align="center" prop="preMoney" v-if="columns[7].visible"/>
        <el-table-column label="商品应付金额" align="center" prop="payProductMoney" v-if="columns[8].visible"/>
        <el-table-column label="优惠券金" align="center" prop="couponMoney" v-if="columns[9].visible"/>
        <el-table-column label="实付金额" align="center" prop="payMoney" v-if="columns[10].visible"/>
        <el-table-column label="收货人" align="center" prop="buyerName" v-if="columns[11].visible"/>
        <el-table-column label="联系电话" align="center" prop="buyerPhone" v-if="columns[12].visible"/>
        <el-table-column label="收货地址" align="center" prop="buyerAddress" v-if="columns[13].visible"/>
        <el-table-column label="状态" align="center" prop="status" v-if="columns[14].visible"/>
        <el-table-column label="活动状态" align="center" prop="acStatus" v-if="columns[15].visible"/>
        <el-table-column label="支付状态：0:未支付，1：已支付" align="center" prop="payStatus" v-if="columns[16].visible"/>
        <el-table-column label="邮费" align="center" prop="postagePrice" v-if="columns[17].visible"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"
            v-if="columns[20].visible"
        >
          <template #default="scope">
            <el-button
                size="mini"
                type="text"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['mall:order:edit']"
            >修改
            </el-button>
            <el-button
                size="mini"
                type="text"
                icon="Document"
                @click="handleDetails(scope.row)"
                v-hasPermi="['mall:order:query']"
            >详情
            </el-button>
            <el-button
                size="mini"
                type="text"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['mall:order:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
      <div class="line-magin"></div>
    </div>
    <!-- 添加或修改商品订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单名" prop="orderName">
          <el-input v-model="form.orderName" placeholder="请输入订单名"/>
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号"/>
        </el-form-item>
        <el-form-item label="支付订单号" prop="payOrderNo">
          <el-input v-model="form.payOrderNo" placeholder="请输入支付订单号"/>
        </el-form-item>
        <el-form-item label="客户" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入客户"/>
        </el-form-item>
        <el-form-item label="订单总金额" prop="orderTotalMoney">
          <el-input v-model="form.orderTotalMoney" placeholder="请输入订单总金额"/>
        </el-form-item>
        <el-form-item label="购物金" prop="shopMoney">
          <el-input v-model="form.shopMoney" placeholder="请输入购物金"/>
        </el-form-item>
        <el-form-item label="优惠金" prop="preMoney">
          <el-input v-model="form.preMoney" placeholder="请输入优惠金"/>
        </el-form-item>
        <el-form-item label="商品应付金额" prop="payProductMoney">
          <el-input v-model="form.payProductMoney" placeholder="请输入商品应付金额"/>
        </el-form-item>
        <el-form-item label="优惠券金" prop="couponMoney">
          <el-input v-model="form.couponMoney" placeholder="请输入优惠券金"/>
        </el-form-item>
        <el-form-item label="实付金额" prop="payMoney">
          <el-input v-model="form.payMoney" placeholder="请输入实付金额"/>
        </el-form-item>
        <el-form-item label="收货人" prop="buyerName">
          <el-input v-model="form.buyerName" placeholder="请输入收货人"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="buyerPhone">
          <el-input v-model="form.buyerPhone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="收货地址" prop="buyerAddress">
          <el-input v-model="form.buyerAddress" placeholder="请输入收货地址"/>
        </el-form-item>
        <el-form-item label="邮费" prop="postagePrice">
          <el-input v-model="form.postagePrice" placeholder="请输入邮费"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 商品订单详情对话框 -->
    <el-dialog class="detail-form" :title="title" v-model="open2" width="700px" append-to-body>
      <el-form ref="orderDetail" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="订单名" prop="orderName">
        {{ form.orderName }}
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        {{ form.orderNo }}
      </el-form-item>
      <el-form-item label="支付订单号" prop="payOrderNo">
        {{ form.payOrderNo }}
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        {{ form.customerId }}
      </el-form-item>
      <el-form-item label="订单总金额" prop="orderTotalMoney">
        {{ form.orderTotalMoney }}
      </el-form-item>
      <el-form-item label="购物金" prop="shopMoney">
        {{ form.shopMoney }}
      </el-form-item>
      <el-form-item label="优惠金" prop="preMoney">
        {{ form.preMoney }}
      </el-form-item>
      <el-form-item label="商品应付金额" prop="payProductMoney">
        {{ form.payProductMoney }}
      </el-form-item>
      <el-form-item label="优惠券金" prop="couponMoney">
        {{ form.couponMoney }}
      </el-form-item>
      <el-form-item label="实付金额" prop="payMoney">
        {{ form.payMoney }}
      </el-form-item>
      <el-form-item label="收货人" prop="buyerName">
        {{ form.buyerName }}
      </el-form-item>
      <el-form-item label="联系电话" prop="buyerPhone">
        {{ form.buyerPhone }}
      </el-form-item>
      <el-form-item label="收货地址" prop="buyerAddress">
        {{ form.buyerAddress }}
      </el-form-item>
      <el-form-item label="状态" prop="status">
        {{ form.status }}
      </el-form-item>
      <el-form-item label="活动状态" prop="acStatus">
        {{ form.acStatus }}
      </el-form-item>
      <el-form-item label="支付状态：0:未支付，1：已支付" prop="payStatus">
        {{ form.payStatus }}
      </el-form-item>
      <el-form-item label="邮费" prop="postagePrice">
        {{ form.postagePrice }}
      </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
  import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/mall/order";
  const { proxy } = getCurrentInstance();

  // 遮罩层
  const loading = ref(true);
  // 导出遮罩层
  const exportLoading = ref(false);
  // 选中数组
  const ids = ref([]);
  // 非单个禁用
  const single = ref(true);
  // 非多个禁用
  const multiple = ref(true);
  // 切换搜索条件
  const showSearch = ref(false);
  // 总条数
  const total = ref(0);
  // 商品订单表格数据
  const orderList = ref([]);
  // 弹出层标题
  const title = ref("");
  // 弹出层
  const open = ref(false);
  const open2 = ref(false);
  const data = reactive({
    // 列信息
    columns: [
      {key: 0, label: `订单Id`, visible: true},
      {key: 1, label: `订单名`, visible: true},
      {key: 2, label: `订单编号`, visible: true},
      {key: 3, label: `支付订单号`, visible: true},
      {key: 4, label: `客户`, visible: true},
      {key: 5, label: `订单总金额`, visible: true},
      {key: 6, label: `购物金`, visible: true},
      {key: 7, label: `优惠金`, visible: true},
      {key: 8, label: `商品应付金额`, visible: true},
      {key: 9, label: `优惠券金`, visible: true},
      {key: 10, label: `实付金额`, visible: true},
      {key: 11, label: `收货人`, visible: true},
      {key: 12, label: `联系电话`, visible: true},
      {key: 13, label: `收货地址`, visible: true},
      {key: 14, label: `状态（ 0:待付款 1：待发货，2：待收货，3：已完成，4，退货/售后，5：用户取消支付 6：超时取消支付 )`, visible: true},
      {key: 15, label: `活动状态（0：普通单，1：年会商品订单）`, visible: true},
      {key: 16, label: `支付状态：0:未支付，1：已支付`, visible: true},
      {key: 17, label: `邮费`, visible: true},
      {key: 18, label: `创建时间`, visible: true},
      {key: 19, label: `更新时间`, visible: true},
      {key: 20, label: `操作`, visible: true},
    ],
    // 表单参数
    form:{},
    // 查询参数
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      orderName: null,
      orderNo: null,
      payOrderNo: null,
      customerId: null,
      orderTotalMoney: null,
      shopMoney: null,
      preMoney: null,
      payProductMoney: null,
      couponMoney: null,
      payMoney: null,
      buyerName: null,
      buyerPhone: null,
      buyerAddress: null,
      status: null,
      acStatus: null,
      payStatus: null,
      postagePrice: null,
    },
    rules: {
    }
  });
  const { columns, queryParams, form, rules } = toRefs(data);

  /** 查询商品订单列表 */
  function getList() {
    loading.value = true;
    listOrder(queryParams.value).then(response => {
      orderList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  /** 取消按钮 */
  function cancel() {
    open.value = false;
    open2.value = false;
    reset();
  }
  /** 表单重置 */
  function reset() {
    form.value = {
      orderId: null,
      orderName: null,
      orderNo: null,
      payOrderNo: null,
      customerId: null,
      orderTotalMoney: null,
      shopMoney: null,
      preMoney: null,
      payProductMoney: null,
      couponMoney: null,
      payMoney: null,
      buyerName: null,
      buyerPhone: null,
      buyerAddress: null,
      status: "0",
      acStatus: "0",
      payStatus: "0",
      postagePrice: null,
      createTime: null,
      updateTime: null,
    };
    proxy.resetForm("userForm");
  }
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  /** 切换按钮操作 */
  function changSearch() {
    showSearch.value = !showSearch.value;
  }
  /** 多选框选中数据 */
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.orderId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加商品订单";
  }
  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const orderId = row.orderId || ids.value
    getOrder(orderId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改商品订单";
    });
  }
  /** 详情按钮操作 */
  function handleDetails(row) {
    reset();
    const orderId = row.orderId || ids.value
    getOrder(orderId).then(response => {
      form.value = response.data;
      open2.value = true;
      title.value = "修改商品订单";
    });
  }
  /** 提交按钮操作 */
  function submitForm() {
    proxy.$refs["orderForm"].validate(valid => {
      if (valid) {
        if (form.value.orderId) {
          updateOrder(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addOrder(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    })
  }
  /** 删除按钮操作 */
  function handleDelete(row) {
    const orderIds = row.orderId || ids.value;
    proxy.$modal.confirm('是否确认删除商品订单编号为\"' + orderIds + '\"的数据项?').then(() => {

      return delOrder(orderIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  /** 导出按钮操作 */
  function handleExport() {
    exportLoading.value = true;
    proxy.download("system/post/export", {
      ...queryParams.value
    }, `商品订单表_${new Date().getTime()}.xlsx`).then(response => {
      exportLoading.value = false;
    }).catch(error => {
      exportLoading.value = false;
    });
  }
  getList();
</script>
